<template>
  <div>
    <BasicTable @register="registerTable" :loading="true">
      <!-- 标记 -->
      <template #statusFlag="{ record }">
        <template v-if="record.flag == 0">
          <Tag :color="'green'">启用</Tag>
        </template>
        <template v-if="record.flag == 5">
          <Tag :color="'yellow'">禁用</Tag>
        </template>
        <template v-if="record.flag == 9">
          <Tag :color="'red'">已删除</Tag>
        </template>
      </template>

      <template #toolbar>
        <a-button type="primary" @click="onAddProject"> 新增 </a-button>
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: onEditProject.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              label: '删除',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: onDeleteProject.bind(null, record),
              },
            },
            {
              icon: 'ant-design:audit-outlined',
              label: '项目人员管理',
              color: 'warning',
              onClick: onUsersTable.bind(null, record),
            },
            {
              icon: 'ant-design:form-outlined',
              label: '设置参数',
              color: 'success',
              onClick: onSetParams.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>

    <addOrUpdateProject @register="registerAddOrUpdateForm" @success="afterSuccess" />
    <usersTable @register="registerUsersTable" @success="afterSuccess" />
    <SetProjectParam @register="registerSetParamsForm" @success="afterSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, provide } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';

  import { OperationTypeEnum } from '/@/enums/appEnum';
  import { useMessage } from '/@/hooks/web/useMessage';

  import { queryProjects, deleteProject } from '/@/api/project/project';
  import { projectTableColumns, queryConditionFormSchema } from './project.data';

  import { Tag } from 'ant-design-vue';

  //引入子组件
  import addOrUpdateProject from './AddOrUpdateProject.vue';
  import usersTable from './ProjectUsersTable.vue';
  import SetProjectParam from './SetProjectParam.vue';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  //使用modal弹出窗口
  import { useModal } from '/@/components/Modal';
  const { createMessage } = useMessage();

  const [registerAddOrUpdateForm, { openModal: openAddOrUpdateForm }] = useModal();
  const [registerUsersTable, { openModal: openUsersTable }] = useModal();
  const [registerSetParamsForm, { openModal: openSetParamsForm }] = useModal();

  export default defineComponent({
    name: 'ProjectManagement',
    components: { BasicTable, TableAction, Tag, addOrUpdateProject, usersTable, SetProjectParam },
    setup() {
      //定义模块的名称，提供给子孙组件获取使用
      const moduleName = ref('项目管理');
      provide('moduleName', moduleName);

      /**
       * 请求后台查询接口返回数据显示
       */
      const [registerTable, { reload }] = useTable({
        title: '项目管理',
        api: queryProjects,
        columns: projectTableColumns, //表头数据
        formConfig: {
          labelWidth: 50, //设置查询表单中label的宽度
          schemas: queryConditionFormSchema, // form查询表单
          showResetButton: false, //查询条件部分中是否显示重置按钮
        },
        useSearchForm: true,
        showTableSetting: true, //是否显示表格设置
        showIndexColumn: true,
        canResize: true,
        bordered: true,
        striped: false,
        actionColumn: {
          width: 300,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
        pagination: false, //关闭分页
        afterFetch: (res) => {
          try {
            let result = res.map((item) => {
              if (item.params && item.params.length > 0) {
                let param = JSON.parse(item.params);
                return { ...item, ...param.displayinfo };
              } else return item;
            });
            return result;
          } catch (e) {
            createMessage.error('配置json解析错误');
          }
        },
      });

      /**
       * 打开新增窗口
       */
      function onAddProject() {
        //使用props传递数据至子组件，在data参数中指定
        openAddOrUpdateForm(true, { op: OperationTypeEnum.ADD });
      }

      /**
       * 打开编辑窗口
       */
      function onEditProject(record: Recordable) {
        openAddOrUpdateForm(true, { op: OperationTypeEnum.EDIT, dataId: record.code });
      }

      /**
       * 打开项目人员管理弹窗
       */
      function onUsersTable(record: Recordable) {
        openUsersTable(true, { op: '项目人员管理', dataId: record.code });
      }

      /**
       * 删除
       */
      function onDeleteProject(record: Recordable) {
        deleteProject(record.code)
          .then((res) => {
            console.log('res', res);
            //res判断结果：同时==undefined和null
            if (res == undefined && (res == null || Object.keys(res).length == 0)) {
              createMessage.success('删除成功。', 2);
              afterSuccess();
            }
          })
          .catch((ex) => {
            console.log('catch ', ex);
            createMessage.error(ex.msg);
          });
      }

      /**
       * 设置参数
       */
      function onSetParams(record: Recordable) {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '项目参数',
          operateString: '配置',
          dataId: record.code,
        };
        openSetParamsForm(true, propsData);
      }

      /**
       * 弹窗确认操作之后刷新页面
       */
      function afterSuccess() {
        reload();
      }

      return {
        moduleName,
        registerTable,
        afterSuccess,
        registerAddOrUpdateForm,
        registerUsersTable,
        registerSetParamsForm,
        onAddProject,
        onEditProject,
        onUsersTable,
        onDeleteProject,
        onSetParams,
      };
    },
  });
</script>
